#define  _CRT_SECURE_NO_WARNINGS
#include "logger.h"

#include <cstring>
#include <cstdio>
#include <cstdlib>

Logger::Logger(const char* Filename)
{
	Enable();

	strncpy(filename, Filename, 255);
	file = fopen(filename, "w");

	Msg("Logfile opened.\n");
}

Logger::~Logger()
{
	fclose(file);
	Msg("Logfile closed.\n");
}

void Logger::Error(const char* Message, Logger::Error_type Type)
{	
	if(logging == false)
		return;

	if(Type == CRITICAL) 
		Msg("!! CRITICAL ERROR !!\n");
	else if(Type == NORMAL) 
		Msg("Error: ");

	Msg(Message);

	if(Type == CRITICAL) exit(1);		
}
void Logger::Msg(const char* Message)
{
	if(logging == false)
		return;

	fputs(Message, file);

	fflush(file);
}

void Logger::Enable()
{
	logging = true;
}
void Logger::Disable()
{
	logging = false;
}
